package cn.qzuie.smart.module.dorm.controller.admin.assettype;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.qzuie.smart.framework.common.pojo.PageParam;
import cn.qzuie.smart.framework.common.pojo.PageResult;
import cn.qzuie.smart.framework.common.pojo.CommonResult;
import cn.qzuie.smart.framework.common.util.object.BeanUtils;
import static cn.qzuie.smart.framework.common.pojo.CommonResult.success;

import cn.qzuie.smart.framework.excel.core.util.ExcelUtils;

import cn.qzuie.smart.framework.apilog.core.annotation.ApiAccessLog;
import static cn.qzuie.smart.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.qzuie.smart.module.dorm.controller.admin.assettype.vo.*;
import cn.qzuie.smart.module.dorm.dal.dataobject.assettype.AssetTypeDO;
import cn.qzuie.smart.module.dorm.service.assettype.AssetTypeService;

@Tag(name = "管理后台 - 资产类型")
@RestController
@RequestMapping("/dorm/asset-type")
@Validated
public class AssetTypeController {

    @Resource
    private AssetTypeService assetTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建资产类型")
    @PreAuthorize("@ss.hasPermission('dorm:asset-type:create')")
    public CommonResult<Long> createAssetType(@Valid @RequestBody AssetTypeSaveReqVO createReqVO) {
        return success(assetTypeService.createAssetType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资产类型")
    @PreAuthorize("@ss.hasPermission('dorm:asset-type:update')")
    public CommonResult<Boolean> updateAssetType(@Valid @RequestBody AssetTypeSaveReqVO updateReqVO) {
        assetTypeService.updateAssetType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资产类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dorm:asset-type:delete')")
    public CommonResult<Boolean> deleteAssetType(@RequestParam("id") Long id) {
        assetTypeService.deleteAssetType(id);
        return success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "获得资产类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dorm:asset-type:query')")
    public CommonResult<AssetTypeRespVO> getAssetType(@RequestParam("id") Long id) {
        AssetTypeDO assetType = assetTypeService.getAssetType(id);
        return success(BeanUtils.toBean(assetType, AssetTypeRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得资产类型列表")
    @PreAuthorize("@ss.hasPermission('dorm:asset-type:query')")
    public CommonResult<List<AssetTypeRespVO>> getAssetTypeList(@Valid AssetTypeListReqVO listReqVO) {
        List<AssetTypeDO> list = assetTypeService.getAssetTypeList(listReqVO);
        return success(BeanUtils.toBean(list, AssetTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资产类型 Excel")
    @PreAuthorize("@ss.hasPermission('dorm:asset-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAssetTypeExcel(@Valid AssetTypeListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<AssetTypeDO> list = assetTypeService.getAssetTypeList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "资产类型.xls", "数据", AssetTypeRespVO.class,
                        BeanUtils.toBean(list, AssetTypeRespVO.class));
    }
    @GetMapping("/all")
    @Operation(summary = "获得所有资产类型")
    @PreAuthorize("@ss.hasPermission('dorm:asset-type:query')")
    public  CommonResult<List<AssetTypeRespVO>> all(){

        List<AssetTypeDO> assetTypeDOS=assetTypeService.all();

        return  success(BeanUtils.toBean(assetTypeDOS,AssetTypeRespVO.class));
    }

}